package com.bw.seckill_module.seckillvalues;

import androidx.databinding.ObservableField;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bw.common.field.EventBusEntity;
import com.bw.common.mvvm.viewmodel.BaseViewModel;
import com.bw.common.providerservice.CheckLoginApi;
import com.bw.common.utils.ToastShowUtils;
import com.bw.network_sdk.BaseEntity;
import com.bw.seckill_module.SecKillRespository;
import com.bw.seckill_module.entity.GoodsEntity;
import com.bw.seckill_module.entity.SecKillEntity;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class SecKillValueViewModel extends BaseViewModel<SecKillRespository> {

    public ObservableField<GoodsEntity>bean = new ObservableField<>();
    private Map<String,Object> secMap;
    private CheckLoginApi checkLoginApi;
    public long time;

    @Override
    public void onResult(BaseEntity entity) {
        if (entity instanceof SecKillEntity){
            ToastShowUtils.showShort(entity.message);
            finishPage();
        }
    }

    public void share(){

    }

    public void carList(){
        ARouter.getInstance().build("/car/carfragment")
                .navigation();
    }

    //发起秒杀
    public void subSeckill(){
        secMap.put("time",time);
        respository.requestSecKill(secMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    public void addCar(){
        if (!checkLoginApi.checkLogin()){
            ARouter.getInstance().build("/user/loginactivity")
                    .navigation();
            return;
        }
        GoodsEntity entity = bean.get();
        secMap = new HashMap<>();
        secMap.put("goodsCount","1");
        secMap.put("goodsDesc",entity.goods_desc);
        secMap.put("goodsIcon",entity.goods_default_icon);
        secMap.put("goodsId",entity.id);
        secMap.put("goodsPrice",entity.goods_default_price);
        secMap.put("userId",checkLoginApi.getUserId());
        //发布eventbus事件到activity获取关联的service下维护的时间
        EventBusEntity eventBusEntity = new EventBusEntity();
        eventBusEntity.action = "secValueTime";
        EventBus.getDefault().post(eventBusEntity);
    }

    @Override
    protected void initData() {
        checkLoginApi = (CheckLoginApi) ARouter.getInstance()
                .build("/user/checklogin").navigation();
    }
}
